package com.navi.mould1.service.impl;

import com.navi.bean.in.ConditionBean;
import com.navi.bean.out.insTask_report.InsTaskReportGridOut;
import com.navi.bean.sql.SqlVo;
import com.navi.mould1.service.IInsTaskReportService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import java.util.List;

import static com.navi.bean.sql.SqlVo.jointInSql;
import static com.navi.core.util.JdbcUtils.mySql_QueryRunner_QueryListBySql;

@Slf4j
@Service
public class InsTaskReportServiceImpl implements IInsTaskReportService {


    public List<InsTaskReportGridOut> getGridData(ConditionBean conditions) {
        final SqlVo sqlVo = new SqlVo();
        if (StringUtils.isEmpty(conditions.getDetail_flag())){
            sqlVo.appendSql("SELECT");
            //sqlVo.appendSql("A.ins_task_stat,");
            sqlVo.appendSql("B.tool_task_stat,");
            sqlVo.appendSql("A.fab_id_fk AS fab_id_fk,");
            sqlVo.appendSql("b_1.data_desc AS fab_id,");
            sqlVo.appendSql("B.tool_id_fk,");
            sqlVo.appendSql("b_2.tool_dsc AS tool_id,");
            sqlVo.appendSql("A.shift_id AS shift_id_fk,");
            sqlVo.appendSql("A.from_date,");
            sqlVo.appendSql("A.to_date,");
            sqlVo.appendSql("B.begin_timestamp,");
            sqlVo.appendSql("B.end_timestamp");
            sqlVo.appendSql("FROM icim_jn.ret_tool_ins_task A");
            sqlVo.appendSql("INNER JOIN ret_tool_task B ON A.evt_seq_id = B.ins_evt_seq_id");
            sqlVo.appendSql("LEFT JOIN (select data_ext,data_desc from bis_data where data_cate = 'FBID') b_1 ON A.fab_id_fk = b_1.data_ext");
            sqlVo.appendSql("LEFT JOIN (select tool_id,tool_dsc from bis_tool) b_2 ON B.tool_id_fk = b_2.tool_id");
            sqlVo.appendSql("WHERE 1=1");
            if (StringUtils.isNotEmpty(conditions.getFab_id())) sqlVo.appendSql("AND A.FAB_ID_FK in" + jointInSql(conditions.getFab_id()));
            if (StringUtils.isNotEmpty(conditions.getBay_id())) sqlVo.appendSql("AND B.TOOL_ID_FK in(select tool_id from icim_jn.bis_tool where bay_id in" + jointInSql(conditions.getBay_id()) + ")");
            if (StringUtils.isNotEmpty(conditions.getTool_id())) sqlVo.appendSql("AND B.TOOL_ID_FK in" + jointInSql(conditions.getTool_id()));
            if (StringUtils.isNotEmpty(conditions.getToolg_id())) sqlVo.appendSql("AND B.TOOL_ID_FK in(select tool_id from icim_jn.bis_tool where root_tool_id in (select tool_id_fk from bis_tool_gr where toolg_id in " + jointInSql(conditions.getToolg_id()) + "))");
            if (StringUtils.isNotEmpty(conditions.getTool_type())) sqlVo.appendSql("AND B.TOOL_ID_FK in(select tool_id from icim_jn.bis_tool where unit_typ in" + jointInSql(conditions.getTool_type()) + ")");
            if (StringUtils.isNotEmpty(conditions.getShift_id())) sqlVo.appendSql("AND A.SHIFT_ID in" + jointInSql(conditions.getShift_id()));
            if (StringUtils.isNotEmpty(conditions.getStart_time())) sqlVo.appendSqlAndAddParam("AND A.FROM_DATE >= ? ", conditions.getStart_time());
            if (StringUtils.isNotEmpty(conditions.getEnd_time())) sqlVo.appendSqlAndAddParam("AND A.FROM_DATE <= ? ", conditions.getEnd_time() + " 23:59:59");
            sqlVo.appendSql("ORDER BY fab_id, tool_id, shift_id desc");
        }else if ("true".equals(conditions.getDetail_flag())){
            sqlVo.appendSql("SELECT");
            sqlVo.appendSql("E.data_dsc as item_name,");
            sqlVo.appendSql("E.u_spec,");
            sqlVo.appendSql("E.l_spec,");
            sqlVo.appendSql("F.evt_seq_id as item_task_stat,");
            sqlVo.appendSql("F.data_value,");
            //sqlVo.appendSql("F.evt_usr,");
            sqlVo.appendSql("G.usr_name AS evt_usr,");
            sqlVo.appendSql("B.tool_task_stat,");
            sqlVo.appendSql("A.fab_id_fk AS fab_id_fk,");
            sqlVo.appendSql("b_1.data_desc AS fab_id,");
            sqlVo.appendSql("B.tool_id_fk,");
            sqlVo.appendSql("b_2.tool_dsc AS tool_id,");
            sqlVo.appendSql("A.shift_id AS shift_id_fk,");
            sqlVo.appendSql("A.from_date,");
            sqlVo.appendSql("A.to_date,");
            sqlVo.appendSql("B.begin_timestamp,");
            sqlVo.appendSql("B.end_timestamp");
            sqlVo.appendSql("FROM icim_jn.ret_tool_ins_task A");
            sqlVo.appendSql("INNER JOIN ret_tool_task B ON A.evt_seq_id = B.ins_evt_seq_id");
            sqlVo.appendSql("LEFT JOIN (select data_ext,data_desc from bis_data where data_cate = 'FBID') b_1 ON A.fab_id_fk = b_1.data_ext");
            sqlVo.appendSql("LEFT JOIN (select tool_id,tool_dsc from bis_tool) b_2 ON B.tool_id_fk = b_2.tool_id");
            sqlVo.appendSql("LEFT JOIN icim_jn.ret_tool_ins_item E ON A.ins_item_id = E.ins_item_id_fk and B.tool_id_fk = E.tool_id_fk");
            sqlVo.appendSql("LEFT JOIN icim_jn.ret_tool_ins_list F ON A.evt_seq_id = F.ins_evt_seq_id and E.tool_id_fk = F.tool_id_fk and E.data_seq_id = F.data_seq_id");
            sqlVo.appendSql("LEFT JOIN bis_user G ON F.evt_usr = G.usr_id");
            sqlVo.appendSql("WHERE 1=1");
            if (StringUtils.isNotEmpty(conditions.getFab_id())) sqlVo.appendSql("AND A.FAB_ID_FK in" + jointInSql(conditions.getFab_id()));
            if (StringUtils.isNotEmpty(conditions.getBay_id())) sqlVo.appendSql("AND B.TOOL_ID_FK in(select tool_id from icim_jn.bis_tool where bay_id in" + jointInSql(conditions.getBay_id()) + ")");
            if (StringUtils.isNotEmpty(conditions.getTool_id())) sqlVo.appendSql("AND B.TOOL_ID_FK in" + jointInSql(conditions.getTool_id()));
            if (StringUtils.isNotEmpty(conditions.getShift_id())) sqlVo.appendSql("AND A.SHIFT_ID in" + jointInSql(conditions.getShift_id()));
            if (StringUtils.isNotEmpty(conditions.getStart_time())) sqlVo.appendSqlAndAddParam("AND A.FROM_DATE >= ? ", conditions.getStart_time());
            if (StringUtils.isNotEmpty(conditions.getEnd_time())) sqlVo.appendSqlAndAddParam("AND A.FROM_DATE <= ? ", conditions.getEnd_time() + " 23:59:59");
            sqlVo.appendSql("order by fab_id, tool_id, shift_id_fk");
        }

        List<InsTaskReportGridOut> resultDataList = mySql_QueryRunner_QueryListBySql(sqlVo, InsTaskReportGridOut.class);

        resultDataList.forEach(data -> {
            String shift = "";
            switch (data.getShift_id_fk()==null?"":data.getShift_id_fk()) {
                case "D": shift = "白班"; break;
                case "N": shift = "夜班"; break;
            }
            data.setShift_id(shift);

            String task_stat = "";
            switch (data.getTool_task_stat()==null?"":data.getTool_task_stat()) {
                case "COMP": task_stat = "完成"; break;
                case "WAIT": task_stat = "等待"; break;
                case "INPR": task_stat = "进行中"; break;
            }
            data.setTool_task_stat(task_stat);
        });
        return resultDataList;
    }

}
